package com.wenbo.wenbo.service.impl;


import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import com.wenbo.wenbo.service.MsgService;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;


/**
 * @author ywb
 * @date 2021年12月08日 10:03
 */
@Service
public class MsgServiceImpl implements MsgService {

    private static final Logger log = LoggerFactory.getLogger(MsgServiceImpl.class);
    @Value("${tencent.appId}")
    private String appId;
    @Value("${tencent.appKey}")
    private String appKey;
    @Value("${tencent.templateId}")
    private String templateId;


    @Override
    public void sendMsg(String phone, String[] params) {
        String sign = "鹏鹏记录日志";
        try {
            SmsSingleSender ssender = new SmsSingleSender(Integer.parseInt(appId), appKey);
            SmsSingleSenderResult result = ssender.sendWithParam("86", phone, Integer.parseInt(templateId), params, sign, "", "");
            log.error(result.errMsg);
        } catch (HTTPException e) {
            // HTTP 响应码错误
            e.printStackTrace();
        } catch (JSONException e) {
            // JSON 解析错误
            e.printStackTrace();
        } catch (IOException e) {
            // 网络 IO 错误
            e.printStackTrace();
        }


    }

    @Override
    public void sendMsg(String sign,String phone, String[] params,Integer templateId) {
        try {
            SmsSingleSender ssender = new SmsSingleSender(Integer.parseInt(appId), appKey);
            SmsSingleSenderResult result = ssender.sendWithParam("86", phone, templateId, params, sign, "", "");
            log.error(result.errMsg);
        } catch (HTTPException e) {
            // HTTP 响应码错误
            e.printStackTrace();
        } catch (JSONException e) {
            // JSON 解析错误
            e.printStackTrace();
        } catch (IOException e) {
            // 网络 IO 错误
            e.printStackTrace();
        }


    }
}
